﻿Imports GMap.NET.WindowsForms
Imports GMap.NET.WindowsForms.Markers
Imports GMap.NET.WindowsForms.ToolTips
Imports GMap.NET

Imports System
Imports System.Net
Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbConnection
Imports System.Data
Imports MySql.Data.MySqlClient
Imports System.IO
Imports GMap.NET.CacheProviders
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Repository

Public Class GeoAddress

    Dim currentMarker As CarMarker
    Dim Door As GMapOverlay
    Dim CurentRectMarker As GMapMarkerCross = Nothing
    Dim center As GMapMarker
    Dim isMouseDown As Boolean = False
    Dim Address As GMapOverlay

    Private Sub GeoAddress_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        My.Settings.GeoAddrPos = Me.Location
        My.Settings.GeoAddrSize = Me.Size
    End Sub

    Private Sub GeoAddress_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Me.Location = My.Settings.GeoAddrPos
        Me.Size = My.Settings.GeoAddrSize

        'Load Data
        Me.F3002TableAdapter.Fill(Me.CADISDataSetAddress.F3002)

        'Local Map cache
        Dim ch As MsSQLPureImageCache = New MsSQLPureImageCache
        ch.ConnectionString = My.Settings.PFleet
        GMaps.Instance.ImageCacheSecond = ch

        GMaps.Instance.Mode = AccessMode.ServerAndCache

        Dim proxy As String = My.Settings.Proxy
        Dim proxyport As Integer = Val(My.Settings.ProxyPort)

        'Setup the proxy only if evalable
        If My.Computer.Network.Ping(My.Settings.Proxy) Then
            Me.GMapControl1.Manager.Proxy = New WebProxy(proxy, proxyport)
        End If

        Me.GMapControl1.MapType = GMap.NET.MapType.BingMap

        Me.GMapControl1.MinZoom = Val(My.Settings.MinZoom)
        Me.GMapControl1.MaxZoom = Val(My.Settings.MaxZoom)
        Me.GMapControl1.Zoom = Val(My.Settings.Zoom)

        'adjust zoom bar
        Me.TrackBarControl1.Properties.Minimum = Val(My.Settings.MinZoom)
        Me.TrackBarControl1.Properties.Maximum = Val(My.Settings.MaxZoom)
        Me.TrackBarControl1.Value = Val(My.Settings.Zoom)

        Address = New GMapOverlay(GMapControl1, "Address")
        GMapControl1.Overlays.Add(Address)


        ' map center
        Me.GMapControl1.SetCurrentPositionByKeywords(My.Settings.PFleetCity)


        center = New GMapMarkerCross(GMapControl1.Position)
        Address.Markers.Add(center)

        Geo.Caption = "Lat: " & Math.Round(Val(Address.Markers(0).Position.Lat), 10) & " / Long: " & Math.Round(Val(Address.Markers(0).Position.Lng), 10)


    End Sub

    Private Sub GMapControl1_OnCurrentPositionChanged(point As GMap.NET.PointLatLng) Handles GMapControl1.OnCurrentPositionChanged
        Address.Markers(0).Position = GMapControl1.Position
        Geo.Caption = "Lat: " & Math.Round(Val(Address.Markers(0).Position.Lat), 10) & " / Long: " & Math.Round(Val(Address.Markers(0).Position.Lng), 10)

    End Sub

    Private Sub GMapControl1_OnMapZoomChanged() Handles GMapControl1.OnMapZoomChanged
        'Address.Markers(0).Position = GMapControl1.Position
        GMapControl1.Position = Address.Markers(0).Position

        Geo.Caption = "Lat: " & Math.Round(Val(Address.Markers(0).Position.Lat), 10) & " / Long: " & Math.Round(Val(Address.Markers(0).Position.Lng), 10)
        Me.TrackBarControl1.Value = Me.GMapControl1.Zoom

    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        Me.F3002TableAdapter.Fill(Me.CADISDataSetAddress.F3002)
        GridView1.BestFitColumns()

    End Sub

    Private Sub UpdateLocationToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles UpdateLocationToolStripMenuItem.Click

        Dim x As String
        Dim text As String

        If GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "ID") > 0 Then

            text = "Are you sure to update this address :           " & vbCrLf & vbCrLf
            text = text & GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "JLNAME") & vbCrLf
            text = text & GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "JLADDR3") & vbCrLf
            text = text & GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "JLPLZ")
            text = text & " " & GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "JLCITY") & vbCrLf & vbCrLf
            text = text & "with those cordinates:" & vbCrLf & vbCrLf
            text = text & "Lat : " & Math.Round(Val(Address.Markers(0).Position.Lat), 10) & vbCrLf
            text = text & "Long: " & Math.Round(Val(Address.Markers(0).Position.Lng), 10)

            x = XtraMessageBox.Show(text, "Location confirmation...", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)

            If x = "1" Then

                Dim ID As String

                Dim CADISCon As New System.Data.SqlClient.SqlConnection(My.Settings.CADISConnectionString)
                Dim CADISCmd As New System.Data.SqlClient.SqlCommand
                Dim CADISReader As SqlClient.SqlDataReader = Nothing

                CADISCmd.Connection = CADISCon
                CADISCon.Open()

                With Me.GridView1

                    ID = .GetRowCellValue(.FocusedRowHandle, "ID")

                    CADISCmd.CommandText = "UPDATE dbo.F3002 SET  " & _
                                                "jlGEOSLAT = " & Math.Round(Val(Address.Markers(0).Position.Lat) * 3600000, 0) & ",  JLGEOSLON = " & Math.Round(Val(Address.Markers(0).Position.Lng) * 3600000, 0) & ", " & _
                                                "JLGEODLAT = " & Math.Round(Val(Address.Markers(0).Position.Lat), 10) & ",  JLGEODLON = " & Math.Round(Val(Address.Markers(0).Position.Lng), 10) & "  " & _
                                                "WHERE ID = " & ID
                    CADISCmd.ExecuteNonQuery()
                    CADISCon.Close()
                    'Update view
                    Me.F3002TableAdapter.Fill(Me.CADISDataSetAddress.F3002)
                    GridView1.BestFitColumns()
                End With
            End If
        End If
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Me.GMapControl1.MapType = GMap.NET.MapType.BingMap
        Me.GMapControl1.ReloadMap()
    End Sub

    Private Sub BarButtonItem3_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        Me.GMapControl1.MapType = GMap.NET.MapType.OpenStreetMap
        Me.GMapControl1.ReloadMap()
    End Sub

    Private Sub BarButtonItem4_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick
        Me.GMapControl1.MapType = GMap.NET.MapType.GoogleSatellite
        Me.GMapControl1.ReloadMap()
    End Sub

    Private Sub BarButtonItem5_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem5.ItemClick
        Me.GMapControl1.MapType = GMap.NET.MapType.GoogleMap
        Me.GMapControl1.ReloadMap()
    End Sub

    Private Sub BarButtonItem6_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem6.ItemClick
        Me.GMapControl1.SetCurrentPositionByKeywords(BarEditItem1.EditValue.ToString)

    End Sub

    Private Sub TrackBarControl1_ValueChanged(sender As Object, e As System.EventArgs) Handles TrackBarControl1.ValueChanged
        Me.GMapControl1.Zoom = Val(Me.TrackBarControl1.Value.ToString)
    End Sub
End Class